use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to the server
    let mut stream = TcpStream::connect("127.0.0.1:6379").await?;

    // Send a PING command
    let ping_cmd = "*1\r\n$4\r\nPING\r\n";
    stream.write_all(ping_cmd.as_bytes()).await?;

    // Read response
    let mut buffer = [0; 1024];
    let n = stream.read(&mut buffer).await?;
    let response = String::from_utf8_lossy(&buffer[..n]);

    println!("Response: {}", response);

    // Send SET command
    let set_cmd = "*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$5\r\nworld\r\n";
    stream.write_all(set_cmd.as_bytes()).await?;

    let n = stream.read(&mut buffer).await?;
    let response = String::from_utf8_lossy(&buffer[..n]);
    println!("SET Response: {}", response);

    // Send GET command
    let get_cmd = "*2\r\n$3\r\nGET\r\n$5\r\nhello\r\n";
    stream.write_all(get_cmd.as_bytes()).await?;

    let n = stream.read(&mut buffer).await?;
    let response = String::from_utf8_lossy(&buffer[..n]);
    println!("GET Response: {}", response);

    Ok(())
}